package com.xyebank.ledger.service.nx.adaper;

import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.stereotype.Component;

import com.xyebank.ledger.db.entitys.LPaymentInf;
import com.xyebank.ledger.service.common.conveter.PayBeanConverter;
import com.xyebank.ledger.service.constant.CommonConstant;
import com.xyebank.ledger.service.constant.ProductConstant;
import com.xyebank.ledger.util.DateConverter;
import com.xyebank.ledger.util.IDUtil;
import com.xyebank.ledger.util.StatusConvertUtil;
import com.xyebank.ledger.vo.deal.LNxCashPaymentInfoVo;

@Component
public class NXPayBeanConverter implements PayBeanConverter<LNxCashPaymentInfoVo> {
    
    // 不能采取注入方式
    private DateConverter dateConverter = new DateConverter();
    
    ModelMapper modelMapper = new ModelMapper();

    class NXPaymentPropertyMap extends PropertyMap<LNxCashPaymentInfoVo, LPaymentInf> {
        protected void configure() { 
            //注意　不要有重复的映射，不然会报错
            map(source.getOrdNo(), destination.getXyOrderNo());
            map(source.getThreePartyOrdNo(), destination.getOrgOrderNo());
            map(source.getMblNo(), destination.getMblNo());
            map(source.getTotalAmount(), destination.getApplyAmount());
            using(dateConverter).map(source.getApplTime(), destination.getApplyTime()); //　日期格式转换错误
            map(source.getCashAmount(), destination.getLoanAmount());
            map(source.getCashTime(), destination.getLoanTime());
        }
    }

    public NXPayBeanConverter() {
        modelMapper.addMappings(new NXPaymentPropertyMap());
    }

    @Override
    public LPaymentInf covert(LNxCashPaymentInfoVo x) {
        LPaymentInf entity = modelMapper.map(x, LPaymentInf.class);
        entity.setId(IDUtil.generateId(x.getId().toString(), "nx"));
        entity.setDataSource(x.getSubOrgNo().equals("01") ? CommonConstant.JRY_STR : CommonConstant.SJD_STR);
        entity.setProductNo(ProductConstant.NX_LOAN_PROD_ID);
        entity.setOrgNo(ProductConstant.NX_LOAN_ORG);
        if(x.getStatus() != null) entity.setLoanStatus(StatusConvertUtil.convertPayStatus(x.getStatus()));
        return entity;
    }
}
